//////////////////////////////////////////////////////////////////////////////////////////////
//Class Name: AccountStandardController
//Autor: Víctor Galván Chávez
//Objective: This class contains all the logic for the employee wizard. This class helps user
//to navigate accross the pages of the wizard.
//////////////////////////////////////////////////////////////////////////////////////////////
global class AccountStandardController {
        
    // Variables
    public Account personAccount;
    public Contact contact;
    public List<Employee_relative_del__c> relatives;
    public Employee_Relative_del__c relative;
    public Commute__c commute;
    public List<Commute__c> commutes;
    public Education__c education;
    public List<Education__c> educations;
    public Work_History__c history;
    public List<Work_History__c> histories;
    public transient blob facePhoto{get; set;}
    public transient blob bodyPhoto{get; set;}
    public transient Attachment attFace{get;set;}
    public transient Attachment attBody{get;set;}
    public String messageFacePhoto;
    public String messageBodyPhoto;
    public String styleFacePhoto;
    public String styleBodyPhoto;
    public String fileNameForFacePhoto{get;set;}
    public String fileNameForBodyPhoto{get;set;}
        public String disabled {get;set;}
        public String immediate{get;set;}
        public List<Course_Participant__c> courses;
     

        //*Objective: This is the constructor for the class in both cases: Create a new employee and
        //editing information of an employee. If the URL contains a parameter id, the information is
        //gonna be preloaded and will be avaliable for the user in the pages of he wizard.//
        public AccountStandardController(){
                disabled='true';
                immediate='true';
                try{
                        Id id = ApexPages.currentPage().getParameters().get('id');
                        if(ApexPages.currentPage().getParameters().get('id')!=null){
                                //String id = ApexPages.currentPage().getParameters().get('id');
                                personAccount = (id==null) ? new Account() : 
                                                                [SELECT Id, Height__c, Weight__c, PersonEmail,
                                                                 CurrencyIsoCode, Birthdate__c, Willing_to_travel__c, Marital_status__c,
                                                                 States_where_I_have_family__c, isPersonAccount, Who_cares_them__c, BillingStreet,
                                                                 BillingCity, BillingState, BillingPostalCode, AccountNumber, Position__c,
                                                                 Current_Store__c, Join_date__c, Coat__c, Vest__c, Pants__c, Shirt__c, Face_Photo_ID__c,
                                                                 Body_Photo_ID__c FROM Account WHERE id=:id];
                                                                 
                                                                 
                                contact = (id==null) ? new Contact() : 
                                [SELECT Id, AccountId, Salutation, FirstName, LastName FROM Contact WHERE AccountId=:id];
                                disabled='false';
                                immediate='false';      
                        }
                }catch(System.QueryException ex){
                        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Id does not exist in Employee') );
                }
        }
////////////////////////////////////////////// Get and Set methods/////////////////////////////////////////////
    
    public Account getpersonAccount() {
    if(personAccount == null) personAccount = new Account();
    return personAccount;
    
    }
    
    public Contact getContact(){
        if(contact==null) contact=new Contact();
        return contact;
    }
    
    public List<Employee_relative_del__c> getRelatives(){
        try{
                if(personAccount.Id!=null){
                relatives = [SELECT Name, Relationship__c, Phone__c, Occupation__c FROM Employee_relative_del__c WHERE Employee__c=: personAccount.Id];
                }
        }catch(Exception ex){
                System.debug('An error has ocurred (AccountStandardController): getRelatives '+ex.getMessage());
        }
        return relatives;
    }
    public Employee_relative_del__c getRelative(){
        //if(relative==null){
                relative=new Employee_relative_del__c();
        //}
        return relative;
    }
    
    public Commute__c getCommute(){
        commute = new Commute__c();
        return commute;
    }
    
    public List<Commute__c> getCommutes(){
        try{
                if(personAccount.Id!=null){
                        commutes = [SELECT Store__c, Transfer_Time__c, Cost__c, Route__c FROM Commute__c WHERE Account__c=: personAccount.Id];
                }
        }catch(Exception ex){
                System.debug('An error has ocurred (AccountStandardController): getCommutes '+ex.getMessage()); 
        }
        return commutes;
    }
    
    public Education__c getEducation(){
        education = new Education__c();
        return education;
    }
    
    public List<Education__c> getEducations(){
        try{
                if(personAccount.Id!=null){
                        educations = [SELECT Study_Level__c, Title__c, Institution_Name__c, From__c, To__c FROM Education__c WHERE Employee__c=: personAccount.Id];
                }
        }catch(Exception ex){
                System.debug('An error has ocurred (AccountStandardController): getEducations '+ex.getMessage());
        }
        return educations;
    }
    
    public Work_History__c getHistory(){
        history = new Work_History__c();
        return history;
    }
    
    public List<Work_History__c> getHistories(){
        try{
                if(personAccount.Id!=null){
                        histories = [SELECT Company__c, Position__c, Reason_for_leaving__c, Seniority_yrs__c FROM Work_History__c WHERE Employee__c=: personAccount.Id];
                }
                }catch(Exception ex){
                        System.debug('An error has ocurred (AccountStandardController): getHistories '+ex.getMessage());
                }       
        return histories;
    }
    
    public Attachment getAttFace(){
        if(attFace==null){attFace = new Attachment();}
        return attFace;
    }
    
    public Attachment getAttBody(){
        if(attBody==null){attBody = new Attachment();}
        return attBody;
    }
    
    public String getMessageFacePhoto(){
        return messageFacePhoto;
    }
    
    public String getMessageBodyPhoto(){
        return messageBodyPhoto;
    }
    
    public String getStyleFacePhoto(){
        return styleFacePhoto; 
    }
    
    public String getStyleBodyPhoto(){
        return styleBodyPhoto;
    }
    
    public String getFileNameForFacePhoto(){
        return fileNameForFacePhoto;
    }
    
    public void setFileNameForFacePhoto(String fileName){
        this.fileNameForFacePhoto = fileName;
    }
    
    public String getFileNameForBodyPhoto(){
        return fileNameForBodyPhoto;
    }
    
    public void setFileNameForBodyPhoto(String fileName){
        this.fileNameForBodyPhoto = fileName;
    }
    
    public List<Course_Participant__c>    getCourses(){
        if(courses==null)
        courses = [SELECT Course__r.Name, Due_date__c, Number_Tried__c, Score__c, Exam_Approved__c FROM Course_participant__c WHERE Employee__c =: personAccount.Id];
        return courses;
    }
    
//////////////////////////////////////////////////////////////DML Methods/////////////////////////////////////////////////////////////////
    
//*Objective: Method that inserts an employee. This method is called when user
//clicks on "Next" button in Step 1 of the employee wizard//1
    public PageReference saveEmployee() {
    
    //Insert the account
    try{
        //System.debug('Id del account: '+personAccount.Id);
                    if(personAccount.Id==null){
                        //Define fields that are in Contact Object and assign to Account
                        personAccount.Salutation=contact.Salutation;
                        personAccount.FirstName=contact.FirstName;
                        //personAccount.FirstName = firstName;
                        personAccount.LastName=contact.LastName;
                        personAccount.RecordTypeId='012E0000000cDcN';
                     
                        //System.debug('------------Saving employee----------');
                        
                        insert personAccount;
                        
                        }
                        //System.debug('////////////////////////////////////////////////////Fecha de empleado: '+personAccount.Birthdate__c);
                        if(personAccount.Birthdate__c == null){
                                return Page.EmployeeWizardStep2;
                        }else{
                                //System.debug('///////////////////////Birthdate received: '+personAccount.Birthdate__c);
                                Integer age = Date.today().year() - Date.valueOf(personAccount.Birthdate__c).year();
                                //System.debug('///////////////////////Age in saveEmployee: '+age);
                                if(age<18){
                                        personAccount.Birthdate__c.addError('The employee has to be at least 18 years old');
                                        return null;
                                }else{
                                        return Page.EmployeeWizardStep2;
                                }
                        }
                        
                        //return Page.EmployeeStep2;
        
    }catch(Exception e){
        System.debug('An error has ocurred (AccountStandardController): Error '+e.getMessage()); 
        ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR, 'Sorry. An error has ocurred'));
        return null;
    }
    
    //return null;
    
    }
    
//////////1 End

//*Objective: Method that inserts an employee relative. This method is called when 
//user clicks on "Add Relative to list" button in Step 2 of the employee wizard//2
    public void saveRelative() {
    try{
    //relative = new Employee_Relative_del__c();
    relative.Employee__c=personAccount.Id;
    //Insert the relative
    
    if(relative.Id==null){
                if(relative.Name==null || relative.Name==''){
                        //relative.Name.addError('');
                        ApexPages.addMessage(new Apexpages.Message(ApexPages.Severity.ERROR, 'Please. Enter a value in FullName field'));
                }else{
                        if(relative.Relationship__c == null || relative.Relationship__c == ''){
                                //relative.Relationship__c.addError('Sorry. You should select a value in Relationship field.');
                                ApexPages.addMessage(new Apexpages.Message(ApexPages.Severity.ERROR, 'Please. Select the relationship'));
                        }else{
                                insert relative;
                        }
                }
      
        }
        
    }catch(Exception e){
        System.debug('An error has ocurred (AccountStandardController): Error '+e.getMessage()); 
        ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR, 'Sorry. An error has ocurred'));
     }
    
    
    }
//////////2 End

//Objective: Method that inserts a commute. This method is called when 
//user clicks on "Add commute to list" button in Step 4 of the employee wizard//3
        public void saveCommute(){
                try{
                //Assign the route to the Employee__c (Account)
                commute.Account__c=personAccount.Id;
                
                        if(commute.Store__c == null){
                                ApexPages.addMessage(new Apexpages.Message(ApexPages.Severity.ERROR, 'Please. Select a value in Store Field'));
                        }else{
                                if(commute.Transfer_Time__c==null){
                                        ApexPages.addMessage(new Apexpages.Message(ApexPages.Severity.ERROR, 'Please. Enter a value in Transfer Time field'));
                                }else{
                                        //System.debug('Inserting commute');
                                        insert commute;
                                }
                        }
                        
                        
                }catch(Exception ex){
                        //System.debug('Id del account: '+personAccount.Id);
                        System.debug('An error has ocurred : AccountStandardController: saveCommute: '+ex.getMessage());
                }
                
                
        }
//////////3 End

//Objective: Method that inserts an education level. This method is called when 
//user clicks on "Add study level to list" button in Step 6 of the employee wizard//4
        public void saveEducation(){
                try{
                //Assign the education to the Employee (Account)
                education.Employee__c = personAccount.Id;
                
                        if(education.Study_Level__c == null || education.Study_Level__c == ''){
                                ApexPages.addMessage(new Apexpages.Message(ApexPages.Severity.ERROR, 'Please. Select a value in Study level field'));
                        }else{
                                if(education.Title__c == null || education.Title__c == ''){
                                        ApexPages.addMessage(new Apexpages.Message(ApexPages.Severity.ERROR, 'Please. Enter a value in Title field'));
                                }else{
                                        if(education.From__c > education.To__c){
                                                ApexPages.addMessage(new Apexpages.Message(ApexPages.Severity.ERROR, 'Sorry. From Date can not be greather than To Date'));
                                        }else{
                                                if(education.From__c == education.To__c && education.From__c!=null & education.To__c != null){
                                                        ApexPages.addMessage(new Apexpages.Message(ApexPages.Severity.ERROR, 'Sorry. From Date can not be equal than To Date'));
                                                }else{
                                                        if(education.To__c > Date.today()){
                                                                ApexPages.addMessage(new Apexpages.Message(ApexPages.Severity.ERROR, 'Sorry. To Date can not be greather than current date'));
                                                        }else{
                                                                //System.debug('Inserting education');
                                                                insert education;
                                                        }
                                                        
                                                }
                                                
                                        }
                                }
                        }
                        
                }catch(Exception ex){
                        System.debug('An error has ocurred : AccountStandardController: saveEducation: '+ex.getMessage());
                }
        }
//////////4 End

//Objective: Method that inserts a work history element. This method is called when 
//user clicks on "Add Work to list" button in Step 7 of the employee wizard//5
        public void saveWorkHistory(){
                try{
                //Assign the Work History to the Employee (Account)
                history.Employee__c = personAccount.Id;
                
                        if(history.Company__c == null || history.Company__c == ''){
                                ApexPages.addMessage(new Apexpages.Message(ApexPages.Severity.ERROR, 'Please. Enter a value in Company field'));
                        }else{
                                if(history.Position__c == null || history.Position__c == ''){
                                        ApexPages.addMessage(new Apexpages.Message(ApexPages.Severity.ERROR, 'Please. Enter a value in Position field'));
                                }else{
                                        insert history;         
                                }
                        }
                        
                }catch(Exception ex){
                        System.debug('An error has ocurred : AccountStandardController: saveWorkHistory: '+ex.getMessage());
                }
        }
//////////5 End

//*Objective: Method that finishes the process of adding employee in the wizard 
//and update fields for Employee(Account/Contact) object. This method is called whether user is adding
//a new employee and clicks on "Finish and Save Employee" button, or user is editing information of an
//employee and clicks on "Quick Save" button//6
        public Pagereference finish(){
                try{
                	String pageUrl = ApexPages.currentPage().getUrl();
                        String pageName = pageUrl.split('apex/')[1];
                        
                        if(pageName.contains('?')){
                                pageName=pageName.split('\\?')[0];
                        }
                        pageName = pageName.toUpperCase();
                	System.debug('#########################################pageName: '+pageName);
                	
                	//Just upload the photos when we are in step 6 and clicks "Quick Save" button
                	if(pageName=='EMPLOYEESTEP6')
                	uploadFacePhoto();uploadBodyPhoto();
                        //System.debug('----------------------------Account: '+personAccount);
                        //System.debug('----------------------------Contact ID: '+contact.Id);
                        //update contact;
                        personAccount.Salutation=contact.Salutation;
                    personAccount.FirstName=contact.FirstName;
                    //personAccount.FirstName = firstName;
                    personAccount.LastName=contact.LastName;
                        upsert personAccount;
                        
                        //return (new ApexPages.Standardcontroller(personAccount)).view();
                        PageReference employeesList = Page.Employees;
                        employeesList.setRedirect(true);
                        return employeesList;
                }catch(Exception ex){
                        System.debug('An error has ocurred : AccountStandardController: finish: '+ex.getMessage());
                        return null;
                }
                
        }
//////////6 End

//Objective: Method that deletes a relative. This method is called when 
//user clicks "Delete" link, next to each relative in the list in Step 2 of the employee wizard//7
        public static void deleteRelative(){
                //System.debug('#####################Entering deleteRelative id = '+ApexPages.currentPage().getParameters().get('idToDelete'));
                //Delete the relative record of the Employee_relative_del__c table
                try{
                        Id idRelativeToDelete = ApexPages.currentPage().getParameters().get('idRelativeToDelete');
                        Employee_relative_del__c relativeToDelete = new Employee_relative_del__c(id = idRelativeToDelete);
                        delete relativeToDelete;
                        //System.debug('Relative deleted');
                }catch(Exception ex){
                        System.debug('An error has ocurred : AccountStandardController: deleteRelative: '+ex.getMessage());
                }
        }
//////////7 End

//Objective: Method that deletes a commute. This method is called when 
//user clicks "Delete" link, next to each commute in the list in Step 4 of the employee wizard//8
        public static void deleteCommute(){
                //Delete the commute record of the Commute__c Table
                try{
                        Id idCommuteToDelete = ApexPages.currentPage().getParameters().get('idCommuteToDelete');
                        Commute__c commuteToDelete = new Commute__c(id=idCommuteToDelete);
                        delete commuteToDelete;
                }catch(Exception ex){
                        System.debug('An error has ocurred : AccountStandardController: deleteCommute: '+ex.getMessage());
                }
        } 
/////////8 End

//Objective: Method that deletes a study level. This method is called when 
//user clicks "Delete" link, next to each education level in the list in Step 6 of the employee wizard//9
        public static void deleteEducation(){
                //Delete the education record of the Education__c table
                try{
                        Id idEducationToDelete = ApexPages.currentPage().getParameters().get('idEducationToDelete');
                        Education__c educationToDelete = new Education__c(id=idEducationToDelete);
                        delete educationToDelete;
                }catch(Exception ex){
                        System.debug('An error has ocurred : AccountStandardController: deleteEducation: '+ex.getMessage());
                }
        }
/////////9 End 

//Objective: Method that deletes a work. This method is called when 
//user clicks "Delete" link, next to each work in the list in Step 7 of the employee wizard//10
        public static void deleteWorkHistory(){
                //Delete the work history record of the Work_History__c table
                try{
                        Id idWorkHistoryToDelete = ApexPages.currentPage().getParameters().get('idWorkHistoryToDelete');
                        Work_History__c workHistoryToDelete = new Work_History__c(id=idWorkHistoryToDelete);
                        delete workHistoryToDelete;
                }catch(Exception ex){
                        System.debug('An error has ocurred : AccountStandardController: deleteWorkHistory: '+ex.getMessage());
                }
        }
/////////10 End

//Objective: Method that updates a relative. This method is called when 
//user change the value of any field in relatives list in Step 2 of the employee wizard//11
        @RemoteAction
        public static void updateRelative(String id, String field, String value){
                //System.debug('Entering method updateRelative');
                
                //Update the relative record of the Employee_relative_del__c table
                try{
                        //Employee_Relative_del__c relativeToUpdate = new Employee_Relative_del__c(id=relative.Id);
                        Employee_Relative_del__c relativeToUpdate = [SELECT Name, Relationship__c, Phone__c, Occupation__c
                                                                                                                FROM Employee_relative_del__c WHERE id =: id];
                        //System.debug('Updating relative...');
                        
                        //Change the value of an specific field depending on the parameter field in the method
                        if(field=='Name'){relativeToUpdate.Name = value;}
                        if(field=='Relationship__c'){relativeToUpdate.Relationship__c = value;}
                        if(field=='Phone__c'){relativeToUpdate.Phone__c = value;}
                        if(field=='Occupation__c'){relativeToUpdate.Occupation__c = value;}
                        
                        //Make sure that Name and Relationship__c fields are not empty0
                        if(relativeToUpdate.Name==''){
                                //System.debug('Entering If Name for Relative Error...');
                                //relativeToUpdate.Name.addError('You should enter a value');
                                //ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.ERROR, 'You should enter a value'));
                        }else{
                                if(relativeToUpdate.Relationship__c==''){
                                        //relativeToUpdate.Relationship__c.addError('You should enter a value');
                                        //ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.ERROR, 'Sorry. Name and Relationship of relatives in Relatives List must not be empty'));
                                }else{
                                        //Finally, update the relative record
                                        update relativeToUpdate;
                                }
                        }
                        
                }catch(Exception ex){
                        System.debug('An error has ocurred : AccountStandardController: updateRelative: '+ex.getMessage());
                }
        }
/////////11 End

//Objective: Method that updates a commute. This method is called when 
//user change the value of any field in commutes list in Step 4 of the employee wizard//12
        @RemoteAction
        public static void updateCommute(String id, String field, String value){
                //System.debug('Entering method updateCommute');
                
                //Update the commute record of the Commute__c table
                try{
                        Commute__c commuteToUpdate = [SELECT Store__c, Transfer_Time__c, Cost__c, Route__c
                                                                                  FROM Commute__c WHERE id =: id];
                        
                        //Change the value of an specific field depending on the parameter field in the method
                        //Use the method getIdForStore to know the Id of the store
                        if(field=='Store__c'){commuteToUpdate.Store__c = getIdForStore(value);}
                        if(field=='Transfer_Time__c'){commuteToUpdate.Transfer_Time__c = Decimal.valueOf(value);}
                        if(field=='Cost__c'){
                                        if(value==''){
                                                value='0';
                                        }
                                        commuteToUpdate.Cost__c = Decimal.valueOf(value);
                                        
                                }
                        if(field=='Route__c'){commuteToUpdate.Route__c = value;}
                        
                        
                        //update commuteToUpdate;
                        //Make sure that Store__c and Transfer_Time__c fields are not empty
                        if(commuteToUpdate.Store__c == null){
                                //commuteToUpdate.Store__c.addError('You should enter a value');
                        }else{
                                if(commuteToUpdate.Transfer_Time__c == null){
                                        //commuteToUpdate.Transfer_Time__c.addError('You should enter a value');
                                }else{
                                        //Finally, update the commute record
                                        update commuteToUpdate;
                                }
                        }
                        
                }catch(Exception ex){
                        System.debug('An error has ocurred : AccountStandardController: updateCommute: '+ex.getMessage());
                }
        }
/////////12 End

//Objective: Method that updates a study level. This method is called when 
//user change the value of any field in study list in Step 6 of the employee wizard//13
        @RemoteAction
        public static void updateEducation(String id, String field, String value){
                //System.debug('Entering method updateEducation');
                
                //Update the education record of the Education__c table
                try{
                        Education__c educationToUpdate = [SELECT Study_level__c, Title__c, Institution_Name__c, From__c, To__c
                                                                                          FROM Education__c WHERE id =: id];
                        //System.debug('Updating education...');
                        //System.debug('Id: '+id+' Field: '+field+' Value: '+value);
                        //Change the value of an specific field depending on the parameter field in the method
                        if(field=='Study_level__c'){educationToUpdate.Study_Level__c = value;}
                        if(field=='Title__c'){educationToUpdate.Title__c = value;}
                        if(field=='Institution_Name__c'){educationToUpdate.Institution_Name__c = value;}
                        if(field=='From__c' && value!=''){educationToUpdate.From__c = Date.parse(value);}
                        //System.debug('Antes de hacer la condición... Field= '+field);
                        if(field=='To__c' && value!=''){educationToUpdate.To__c = Date.parse(value);}
                        
                        //Make sure that Study_level__c and Title__c fields are not empty
                        if(educationToUpdate.Study_Level__c == ''){
                                //educationToUpdate.Study_Level__c.addError('You should enter a value');
                        }else{
                                if(educationToUpdate.Title__c == ''){
                                        //educationToUpdate.Title__c.addError('You should enter a value');
                                }else{
                                        if(educationToUpdate.From__c > educationToUpdate.To__c){
                                                //ApexPages.addMessage(new Apexpages.Message(ApexPages.Severity.ERROR, 'Sorry. From Date can not be greather than To Date'));
                                        }else{
                                                if(educationToUpdate.From__c == educationToUpdate.To__c && educationToUpdate.From__c!=null & educationToUpdate.To__c != null){
                                                        //ApexPages.addMessage(new Apexpages.Message(ApexPages.Severity.ERROR, 'Sorry. From Date can not be equal than To Date'));
                                                }else{
                                                        if(educationToUpdate.To__c > Date.today() || educationToUpdate.From__c > Date.today()){
                                                                //ApexPages.addMessage(new Apexpages.Message(ApexPages.Severity.ERROR, 'Sorry. To Date can not be greather than current date'));
                                                        }else{
                                                                //Finally, update the education record
                                                                //System.debug('Updating education...');
                                                                update educationToUpdate;
                                                        }
                                                }
                                        }
                                        
                                }
                        }
                        
                }catch(Exception ex){
                        System.debug('An error has ocurred : AccountStandardController: updateEducation: '+ex.getMessage());
                }
        }
/////////13 End


//Objective: Method that updates a work element. This method is called when 
//user change the value of any field in work history list in Step 7 of the employee wizard//14
        @RemoteAction
        public static void updateWorkHistory(String id, String field, String value){
                //System.debug('Entering method updateWorkHistory');
                
                //Update a history record of the Work_History__c table
                try{
                        Work_History__c workHistoryToUpdate = [SELECT Company__c, Position__c, Seniority_yrs__c, Reason_for_leaving__c
                                                                                               FROM Work_History__c WHERE id =: id];
                        //System.debug('Updating Work History...');
                        
                        //Change the value of an specific field depending on the parameter field in the method
                        if(field=='Company__c'){workHistoryToUpdate.Company__c = value;}
                        if(field=='Position__c'){workHistoryToUpdate.Position__c = value;}
                        if(field=='Seniority_yrs__c'){
                                if(value==''){
                                        value='0';
                                }
                                workHistoryToUpdate.Seniority_yrs__c = Double.valueOf(value);
                        }
                        if(field=='Reason_for_leaving__c'){workHistoryToUpdate.Reason_for_leaving__c = value;}
                        
                        //Make sure that Company__c and Position__c fields are not empty
                        if(workHistoryToUpdate.Company__c == ''){
                                //workHistoryToUpdate.Company__c.addError('You should enter a value');
                        }else{
                                if(workHistoryToUpdate.Position__c == ''){
                                        //workHistoryToUpdate.Position__c.addError('You should enter a value');
                                }else{
                                        //Finally, update the history record
                                        update workHistoryToUpdate;
                                }
                        }
                        
                }catch(Exception ex){
                        System.debug('An error has ocurred : AccountStandardController: updateWorkHistory: '+ex.getMessage());
                }
        }
/////////14 End

////////////////////////////////////////////////////////////////Upload pictures//////////////////////////////////

//Objective: Method that uploads a photo. This method is called when 
//user clicks "Next" button in Step 5 whether if user is adding a new employee
//or is editing it's information//15
        public boolean uploadFacePhoto(){
                boolean go = true;
                String[] permittedExtensions = new String[]{'jpeg','jpg','gif','tif','png'};
        //System.debug('Entering uploadFacePhoto');
        try{
                        
                        if(facePhoto.size() > 0 && facePhoto.size() < 102400 && Utilities.isValidExtension(fileNameForFacePhoto, permittedExtensions)==true){//Check if the file size is greather than 0 and less than 100KB
                                attFace = new Attachment();
                                //System.debug('Entrando a insertar el attachment de face');
                                attFace.Body = facePhoto;
                                attFace.Name = 'Face photo-'+contact.FirstName+'-'+contact.LastName;
                                attFace.ParentId = personAccount.Id;
                                attFace.ContentType = 'application/jpg';
                                
                                if(personAccount.Face_Photo_ID__c==null){
                                        insert attFace;
                                        personAccount.Face_Photo_ID__c = attFace.Id;
                                        update personAccount;
                                        go = true;
                                        messageFacePhoto = 'Already Uploaded';
                                        styleFacePhoto = 'color:green;';
                                }else{
                                        //Assing the Id that has the user in Face_Photo_ID__c field to know what record of Attachment is gonna be updated
                                        attFace = [SELECT Id FROM Attachment WHERE Id=:personAccount.Face_Photo_ID__c];
                                        if(attFace!=null){
                                        attFace.Body = facePhoto;
                                        update attFace;
                                        go = true;
                                        }
                                }
                        }else{
                                        ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.ERROR, 'Sorry. The file you are uploading for Face Photo should be 100KB or less and It´s only permitted upload files with .jpeg, .jpg, .gif and .tif extensions'));
                                        go = false;
                        }
                
           }catch(Exception ex){
                        System.debug('An error has ocurred : AccountStandardController: uploadFacePhoto: '+ex.getMessage());
                }
        
        return go;
        }
/////////15 End

//Objective: Method that uploads a photo. This method is called when 
//user clicks "Next" button in Step 5 whether if user is adding a new employee
//or is editing it's information//16    
        public boolean uploadBodyPhoto(){
                boolean go = true;
                String[] permittedExtensions = new String[]{'jpeg','jpg','gif','tif','png'};
        try{
                        if(bodyPhoto.size() > 0 && bodyPhoto.size() < 102400 && Utilities.isValidExtension(fileNameForBodyPhoto, permittedExtensions)==true){//Check if the file size is greather than 0 and less than 100KB
                                attBody = new Attachment();
                                //System.debug('Entrando a insertar el attachment de face');
                                attBody.Body = bodyPhoto;
                                attBody.Name = 'Body photo-'+contact.FirstName+'-'+contact.LastName;
                                attBody.ParentId = personAccount.Id;
                                attBody.ContentType = 'application/jpg';
                                
                                if(personAccount.Body_Photo_ID__c==null){
                                        insert attBody;
                                        personAccount.Body_Photo_ID__c = attBody.Id;
                                        update personAccount;
                                        go = true;
                                        messageBodyPhoto = 'Already Uploaded';
                                        styleBodyPhoto = 'color:green;';
                                }else{
                                        //Assing the Id that has the user in Body_Photo_ID__c field to know what record of Attachment is gonna be updated
                                        attBody = [SELECT Id FROM Attachment WHERE Id=:personAccount.Body_Photo_ID__c];
                                        if(attBody!=null){
                                        attBody.Body = bodyPhoto;
                                        update attBody;
                                        go = true;
                                        }
                                }
                        }else{
                                        ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.ERROR, 'Sorry. The file you are uploading for Body Photo should be 100KB or less and It´s only permitted upload files with .jpeg, .jpg, .gif and .tif extensions'));
                                        go = false;
                        }
                
          }catch(Exception ex){
                System.debug('An error has ocurred : AccountStandardController: uploadBodyPhoto: '+ex.getMessage());
          }
          
        return go;
        }
/////////End 16
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////Navigation Methods//////////////////////////////

//*Objetive: Send user to Step 1 in the wizard
    public Pagereference goToStep1(){
        return Page.EmployeeWizardStep1;
    }
//*Objetive: Send user to Step 2 in the wizard
    public PageReference goToStep2(){
        return Page.EmployeeWizardStep2;
    }
//*Objetive: Send user to Step 3 in the wizard
    public PageReference goToStep3(){
        return Page.EmployeeWizardStep3;
    }
//*Objetive: Send user to Step 3 in the wizard
    public Pagereference goToStep4(){
        //In this part we validate that if the employee has children, the field who cares them is filled
        List<Employee_Relative_del__c> employeeRel = [SELECT Id FROM Employee_Relative_del__c WHERE Employee__c=: personAccount.Id AND Relationship__c='Child'];
        if(employeeRel.size()!=0 && personAccount.Who_Cares_Them__c==null){
                ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.ERROR, 'Due the employee has children, please enter a value un Who Cares the children Field'));
                return null;
        }else{
                return Page.EmployeeWizardStep3;
        }
    }
//*Objetive: Send user to Step 4 in the wizard
    public Pagereference goToStep5(){
        return Page.EmployeeWizardStep4;
    }
//*Objetive: Send user to Step 5 in the wizard
    public Pagereference goToStep6(){
        return Page.EmployeeWizardStep5;
    }
//*Objetive: Send user to Step 6 in the wizard
    public Pagereference goToStep7(){
        //Save the photos
        if(uploadFacePhoto() && uploadBodyPhoto()){return Page.EmployeeWizardStep6;}
        return null;
    }
//*Objetive: Send user to Step 7 in the wizard
    public Pagereference goToStep8(){
        return Page.EmployeeWizardStep7;
    }
    
//*Objetive: Send user to employees page. This method is called when user clicks "Cancel" button
    public PageReference cancel(){
        try{
                if(personAccount.Id!=null){
                        if(ApexPages.currentPage().getParameters().get('id')==null)
                        delete personAccount;
                }
                return Page.Employees;
        }catch(Exception ex){
                System.debug('An error has ocurred : AccountStandardController: cancel: '+ex.getMessage());
                return null;
        }
    }

///////////////////////////////////////////////////////////////////////////////

////////////////////////////////////Utilities Methods//////////////////////////////////

//*Objetive: Return the store id depending the name
        public static String getIdForStore(String name){
                Store__c store = [SELECT id FROM Store__c WHERE Name=: name];
                return store.id;
        }

//*Objetive: Method that checks if the employee object is null and redirect to EmployeeWizardStep1 page.
//This methid prevents that the user adds an employee starting in a page different than EmployeeWizardStep1
         public PageReference checkEmployee(){
        if (personAccount == null) {
            PageReference goBack = Page.EmployeeWizardStep1;
            goBack.setRedirect(true);
            return goBack;
            //return null;
        } else {
            return null;    
        }
    }
///////////////////////////////////////////////////////////////////////////////////////

}